package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.VisitorList;
import com.example.prison.service.SystemLogService;
import com.example.prison.service.VisitorListService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "访客记录")
@RestController
@RequestMapping("/api")
public class VisitorListController {

    @Autowired
    private VisitorListService visitorListService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取访客记录")
    @GetMapping("/getVisitorList")
    public Response<PageInfo<VisitorList>> getVisitorList(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                          @Parameter(description = "访客姓名")@RequestParam(required = false) String visitorName,
                                                          @Parameter(description = "开始时间")@RequestParam(required = false) String beginTime,
                                                          @Parameter(description = "结束时间")@RequestParam(required = false) String endTime) {
        PageInfo<VisitorList> pageInfo = visitorListService.get(pageNumber, pageSize, visitorName, beginTime, endTime);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("删除访客记录")
    @PostMapping("/deleteVisitorList")
    public Response<?> deleteVisitorList(@Parameter(description = "预约编号") @RequestBody Integer[] ids) {
        visitorListService.delete(ids);
        log.info("删除访客记录");
        systemLogService.add("删除访客记录");
        return Response.success("删除成功！");

    }

    @ApiOperation("依据id获取访客记录")
    @GetMapping("/getVisitorListById")
    public Response<VisitorList> getVisitorListById(@Parameter(description = "预约编号(id)")@RequestParam Integer departId) {
        VisitorList vistor = visitorListService.getById(departId);
        if(ObjectUtils.isEmpty(vistor)) {
            return Response.error("此预约编号不存在！");
        }
        return Response.success("获取成功！", vistor);
    }

}

